package com.sk.sourcecircle.dialog;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.sk.sourcecircle.R;
import com.sk.sourcecircle.callback.MyCallBack;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by sk on 2017/12/28.
 */

public class CommentDialogFragment extends DialogFragment {


    @BindView(R.id.add_collect)
    ImageView addCollect;
    @BindView(R.id.cancel_collect)
    ImageView cancelCollect;
    Unbinder unbinder;
    @BindView(R.id.ed_comment)
    EditText edComment;

    private boolean isCollect = false;

    public boolean isCollect() {
        return isCollect;
    }

    public void setCollect(boolean collect) {
        isCollect = collect;
    }

    private MyCallBack callBack;

    public MyCallBack getCallBack() {
        return callBack;
    }

    public void setCallBack(MyCallBack callBack) {
        this.callBack = callBack;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_comment, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (isCollect) {
            cancelCollect.setVisibility(View.VISIBLE);
            addCollect.setVisibility(View.INVISIBLE);
        } else {
            cancelCollect.setVisibility(View.INVISIBLE);
            addCollect.setVisibility(View.VISIBLE);
        }

        edComment.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
                    // 先隐藏键盘
                    ((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE))
                            .hideSoftInputFromWindow(v.getWindowToken(),
                                    InputMethodManager.HIDE_NOT_ALWAYS);
                    //进行搜索操作的方法，在该方法中可以加入mEditSearchUser的非空判断
                    if (edComment.getText().toString().length() > 0) {
                        callBack.sendText(edComment.getText().toString());
                        dismiss();
                    } else {
                        Toast.makeText(getActivity(), "发送的消息不能为空!", Toast.LENGTH_SHORT).show();
                    }
                }
                return false;
            }
        });
    }

    public void onStart() {
        super.onStart();
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00000000")));
        WindowManager.LayoutParams layoutParams = getDialog().getWindow().getAttributes();
        layoutParams.gravity = Gravity.BOTTOM;
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        getDialog().getWindow().setAttributes(layoutParams);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.add_collect, R.id.cancel_collect})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.add_collect:
                callBack.sure();
                dismiss();
                break;
            case R.id.cancel_collect:
                break;
        }
    }
}
